using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmAccount : Form
    {
        List<Entities.Login> accountList = new List<Entities.Login>();
        List<Entities.Role> roleList = new List<Entities.Role>();
        Business.LoginService loginService = new Business.LoginService();
        Business.RoleService roleService = new Business.RoleService();
        Entities.Login account = new Entities.Login();
        int iIndex = 0;
        
        public FrmAccount()
        {
            InitializeComponent();
        }

        private void InitializeAccounts()
        {
            //Refresh datagridview
            if (dgvAccounts.Rows.Count > 1)
            {
                int iCount = dgvAccounts.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvAccounts.Rows.RemoveAt(0);
                }
            }
            //
            accountList = loginService.GetAllAccounts();
            foreach (Entities.Login account in accountList)
            {
                string[] temp = { account.LoginID, account.RoleID, account.State.ToString() };
                dgvAccounts.Rows.Add(temp);
            }
            dgvAccounts[0, iIndex].Selected = true;
        }

        private void InitializeRoles()
        {
            roleList = roleService.GetAllRoles();
            foreach (Entities.Role role in roleList)
            {
                 cmbRole.Items.Add(role.RoleID +"--"+ role.RoleDetail );
            }
        }

        private void InitializeStatus()
        {
            cmbStatus.Items.Add("Active");
            cmbStatus.Items.Add("Unactive");
        }

        private string RandomNewPassword()
        {
            int iCount = 0;
            string sNewPassword = string.Empty;
            while (iCount < 3)
            {
                Random random = new Random();
                int iRandom = random.Next(iCount + 11, 100 - iCount);
                sNewPassword += iRandom.ToString();
                iCount++;
            }
            sNewPassword += "@X";
            return sNewPassword;
        }

        private void FindAccountByLoginID(string sLoginID)
        {
            account = loginService.GetAccountByLoginID(sLoginID);
            if (account != null)
            {
                txtUserName.Text = account.LoginID;
                cmbRole.Text = account.RoleID;
                if (account.State > 0)
                    cmbStatus.SelectedIndex = 0;
                else
                    cmbStatus.SelectedIndex = 1;
            }
            else
                MessageBox.Show("This ID doesn't exist");
        }

        private void FindAccountByUserName(string sUserName)
        {
            accountList = loginService.GetAllAccounts();
            if (dgvAccounts.Rows.Count > 1)
            {
                int iCount = dgvAccounts.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvAccounts.Rows.RemoveAt(0);
                }
            }
            foreach (Entities.Login account in accountList)
            {
                if (account.LoginID.ToLower().Contains(sUserName.ToLower()))
                {
                    string[] temp = { account.LoginID, account.RoleID, account.State.ToString() };
                    dgvAccounts.Rows.Add(temp);
                }
            }
        }

        private void UpdateAccount()
        {
            if (Test())
            {
                account.RoleID = (cmbRole.Text.Length > 3 ? cmbRole.Text.Substring(0, 3) : cmbRole.Text);
                account.State = (cmbStatus.SelectedIndex == 0 ? 1 : 0);
                loginService.UpdateAccount(account);
                MessageBox.Show("Updated");
                InitializeAccounts();
            }
        }

        private void ResetPassword()
        {
            string sNewPassword = RandomNewPassword();
            account.Password = sNewPassword;
            txtPassword.Text = sNewPassword;
            loginService.UpdatePasswordOfAccount(account);
        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (cmbRole.Text == string.Empty)
                sInformation += " . Role 's empty" + "\n";
            if (cmbStatus.Text == string.Empty)
                sInformation += " . Status of account 's empty" + "\n";
            if (sInformation != string.Empty)
            {
                MessageBox.Show(sInformation, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bResult = false;
            }
            return bResult;
        }

        private void dgvAccounts_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvAccounts.CurrentRow.Index < dgvAccounts.Rows.Count-1)
            {
                txtPassword.Text = string.Empty;
                iIndex = dgvAccounts.CurrentRow.Index;
                string sLoginID = dgvAccounts.CurrentRow.Cells[0].Value.ToString();
                FindAccountByLoginID(sLoginID);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FindAccountByUserName(txtUserName.Text.Trim());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (account != null)
                UpdateAccount();
            else
                MessageBox.Show("You should choose account which you want to change?");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (account != null)
            {
                if (MessageBox.Show("You want to reset password for user : " + account.LoginID + " ?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    ResetPassword();
                }
            }
            else
                MessageBox.Show("You should choose account which you want to reset password?");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmAccount_Load_1(object sender, EventArgs e)
        {
            InitializeAccounts();
            InitializeRoles();
            InitializeStatus();
        }

        private void dgvAccounts_KeyDown(object sender, KeyEventArgs e)
        {
        }
    }
}